package com.attendancesystem.controller;

import com.attendancesystem.model.Attendance;
import com.attendancesystem.service.AttendanceDetailService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;



import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "GetAttendanceDetailController", value = "/GetAttendanceDetailController")
public class GetAttendanceDetailController extends HttpServlet {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
     doPost(req, rep);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//        String lectureId = req.getParameter("lectureId");
        rep.setContentType("application/json;charset=UTF-8"); // 增加字符集定义
        PrintWriter out = rep.getWriter();
        ObjectNode responsejson = objectMapper.createObjectNode();
        JsonNode rootNode = objectMapper.readTree(req.getReader());
//        String course = req.getParameter("course");
//        String time = req.getParameter("time");
        String course = rootNode.get("course").asText();
        String time = rootNode.get("time").asText();
        if (course == null || course.isEmpty() || time == null || time.isEmpty()) {
            sendError(rep, HttpServletResponse.SC_BAD_REQUEST, "参数不完整");
            return;
        }
        List<Attendance> attendanceList = new ArrayList<>();
        ArrayNode arrayNode = responsejson.putArray("students");
        try {
            AttendanceDetailService attendanceDetailService = new AttendanceDetailService();
            attendanceList =attendanceDetailService.getAttendance(course, time);
            for (Attendance attendance : attendanceList){
                ObjectNode json = arrayNode.addObject();
                json.put("attendanceId", attendance.getAttendanceId());

                json.put("studentId", attendance.getStudentId());
                json.put("courseName", attendance.getCourseName());
                json.put("attendanceDate", attendance.getAttendanceDate().toString());
                json.put("status", attendance.getStatus().getChineseName());

            }

            rep.setHeader("Access-Control-Allow-Origin", "*");
            rep.setHeader("Access-Control-Allow-Methods", "POST");
            out.print(responsejson);

//            System.out.println("attendanceList:"+attendanceList);
            // 在后端添加调试日志
            System.out.println("Response JSON: " + responsejson.toString());


//            req.setAttribute("attendanceList", attendanceList);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        System.out.println("course:"+course);
        System.out.println("time:"+time);


    }
    private void sendError(HttpServletResponse resp, int code, String msg)
            throws IOException {
        resp.setStatus(code);
        resp.getWriter().print("{\"error\":\"" + msg + "\"}");
    }
}
